import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description：
 * User：user
 * DATE:2021-11-10
 * Time:0:00
 */
public class Testheap {
    public  int[] elem;
    public  int usedSize;
    public  Testheap(){
        this.elem = new int[10];
    }

    public void shifDown(int parent,int len){
        int child = 2 * parent + 1;
        while (child < len){
            if(child + 1 < len && this.elem[child] < this.elem[child + 1]){
                child++;
            }
            if(this.elem[child] > this.elem[parent]){
                int tmp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = tmp;
                parent = child;
                child = 2 * parent + 1;
            }
        }
    }
    public  void cerateTree(int[] array){
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            this.usedSize++;
        }

        //从最后一颗子树进行向下转化
        for (int parent = (this.elem.length - 1 - 1) / 2; parent >= 0; parent--) {
            shifDown(parent,this.elem.length);
        }
    }

    public  void shiftUp(int child,int len){
        int parent = (child - 1) / 2;
        while (parent >= 0){
            if(this.elem[child] > this.elem[parent]){
                int tmp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = tmp;
                child = parent;
                parent = (child - 1) / 2;
            }else{
                break;
            }
        }
    }
    public  void push(int val){
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2 * this.elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
        shiftUp(this.usedSize-1,this.elem.length);
    }
    public boolean isFull(){
        return  this.elem.length == this.usedSize;
    }
    public  int pop(){
        if(isEmpty())return  -1;
        int tmp = this.elem[0];
        this.elem[0] = this.elem[this.elem.length - 1];
        this.elem[this.elem.length - 1] = tmp;
        this.usedSize--;
        shifDown(0,this.elem.length);
        return tmp;
    }
    public  boolean isEmpty(){
        return  this.elem.length == 0;
    }

    public int peek(){
        if(isEmpty())return  -1;
        return this.elem[0];
    }
}
